package com.mq.work;

import com.mq.config.ConnectUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;

/**
 * @author hezhaoxiang
 * @description //work 模型 生产者
 * @date 15:51 2020/9/6
 **/

public class HelloWorldSend {

    //队列名称
    private final static String QUEUE_NAME = "HELLO";

    private static void sendMsg() throws URISyntaxException, KeyManagementException, TimeoutException, NoSuchAlgorithmException, IOException {
        //1. 获取连接
        Connection connect = ConnectUtils.getConnect();
        //2. 创建信道
        Channel channel = connect.createChannel();
        //3. 设置队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //4. 发送(批量)消息
        for (int i = 0; i < 50; i++) {
            String msg = "work_test_msg" + i;
            channel.basicPublish("", QUEUE_NAME, MessageProperties.TEXT_PLAIN, msg.getBytes());
            System.out.println("work 模型 成功发送消息:" + msg);
        }
        //5. 关闭信道
        channel.close();
        //6. 关闭连接
        connect.close();
    }

    public static void main(String[] args) throws KeyManagementException, TimeoutException, NoSuchAlgorithmException, IOException, URISyntaxException {
        HelloWorldSend.sendMsg();
    }
}
